package lemon1.util.transaction;

import lemon1.util.JDBCUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.*;

public class TransactionTest {

    public static void main(String[] args) {
//        Connection connection = null;
//        try {
//            connection = JDBCUtils.getConnection();
//            //System.out.println(connection.getAutoCommit());
//            connection.setAutoCommit(false);
//            String sql1 = "update user_table set balance = balance - 100 where user = ?";
//            update(connection, sql1, "AA");
//            //System.out.println(10 / 0);
//            String sql2 = "update user_table set balance = balance + 100 where user = ?";
//            update(connection, sql2, "BB");
//            connection.commit();
//        } catch (Exception e) {
//            e.printStackTrace();
//            try {
//                connection.rollback();
//            } catch (SQLException ex) {
//                ex.printStackTrace();
//            }
//        } finally {
//            try {
//                connection.setAutoCommit(true);
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//            JDBCUtils.closeResource(connection, null);
//        }

    }

    public static void testTransactionSelect() throws SQLException, IOException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        String sql = "select user,password,balance from user_table where user = ?";
        User cc = getInstance(connection, User.class, sql, "CC");
        System.out.println(cc);
    }

    public static void testTransactionUpdate() throws SQLException, IOException, ClassNotFoundException, InterruptedException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "update user_table set balance = ? where user = ?";
        update(connection, sql, 5000);
        Thread.sleep(15 * 1000);
        System.out.println("");
    }

    public static <T> T getInstance(Connection connection, Class<T> clazz, String sql, Object ...args) {
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            resultSet = ps.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            if (resultSet.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    Object columnValue = resultSet.getObject(i + 1);
                    String columnName = metaData.getColumnLabel(i + 1);
                    Field declaredField = clazz.getDeclaredField(columnName);
                    declaredField.setAccessible(true);
                    declaredField.set(t, columnValue);
                }
                return t;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(null, ps, resultSet);
        }
        return null;
    }


    public static int update(Connection connection, String sql, Object...args) {
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            return ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(null, ps);
        }
        return 0;
    }
}
